(function() {
    var next_openid = '';
    Path.on('/admin/__basename__.html', function(_current_location) {
        App.set('admin_title', '粉丝列表');

        var qs = _current_location.query;
        next_openid = qs.get('next_openid');

    });

    coAjax.get(apis.wx.user.list, function(result) {
        App.set('$Cache.admin.funs.list', result.list);
    }, function(errCode, errMsg, err) {
        alert('warn', errMsg);
    });

    App.set('$Event.admin.funs.detail', function(e, vm) {
        var openid = typeof vm.get("$This") == 'object' ? vm.get('openid') : vm.get("$This");
        coAjax.get(apis.wx.user.info + openid, function(result) {
            vm.set('$This', result);
        });
    });

    // 更新备注
    App.set('$Event.admin.funs.updateRemark', function(e, vm) {
        coAjax.put(apis.wx.user.remark + vm.get('openid'), { remark: vm.get('remark') }, function(result) {
            console.log(result);
            result.errorcode ? alert('warn', '更新备注失败，请尝试刷新access_token') :
                alert('success', '备注更新成功');
        })
    });

    App.set('$Event.admin.funs.refreshList', function() {
        coAjax.get(apis.wx.user.refresh, { next_openid: next_openid }, function(result) {
                App.set('$Cache.admin.funs.list', result);
                console.log(result)
            },
            function(errCode, errMsg, err) {
                alert('warn', errMsg);
            });
    });
}());
